package com.example.demo.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.user.entity.UUser;
import com.example.demo.user.service.UUserService;
import com.example.demo.util.result.Result;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * <p>
 * 用户表; 前端控制器
 * </p>
 *
 * @author 涛
 * @since 2024-08-01
 */
@RestController
@RequestMapping("/uUser")
@Tag(name = "uUser", description = "用户表")
public class UUserController {
    @Autowired
    private UUserService uUserService;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 获取用户模块列表
     */
    @GetMapping("/getUserList")
    @Operation(summary = "获取用户列表接口", description = "获取用户列表接口")
    public Result<?> getUserList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                    @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                    UUser user){
        Page<UUser> page = new Page<>(current,size);
        QueryWrapper<UUser> queryWrapper = new QueryWrapper<>(user);
        return Result.ok(uUserService.page(page,queryWrapper));
    }

    /**
     * 根据ID查询用户
     */
    @GetMapping("/getUserById")
    @Operation(summary = "根据id查询用户明细", description = "根据id查询用户明细")
    public Result<?> getUserById(Integer id){
        return Result.ok(uUserService.getById(id));
    }

    /**
     * 修改用户
     */
    @PutMapping("/updateUser")
    @Operation(summary = "根据id修改用户明细", description = "根据id修改用户明细")
    @Transactional
    public Result<?> updateUser(@RequestBody UUser user){
        return Result.ok(uUserService.updateById(user));
    }

    /**
     * 新增用户
     */
    @PostMapping("/addUser")
    @Operation(summary = "新增用户明细", description = "新增用户明细")
    public Result<?> addUser(@RequestBody UUser user){
        return Result.ok(uUserService.save(user));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/deleteUserById")
    @Operation(summary = "删除用户", description = "删除用户")
    @Transactional
    public Result<?> deleteUserById(Integer id){
        return Result.ok(uUserService.removeById(id));
    }

    public Result<?> wxLogin(@RequestBody UUser user){
        String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + "?accessToken" +
                "&openid=" + "?openid";

        ResponseEntity<String> userInfoResponse = restTemplate.getForEntity(userInfoUrl, String.class);
        JSONObject userInfo = new JSONObject(userInfoResponse.getBody());

        String nickname = userInfo.getString("nickname");
        String headimgurl = userInfo.getString("headimgurl");
        return Result.ok();
    }

    private String generateJwtToken(String openid) {
        // 使用JWT库生成Token
        return Jwts.builder()
                .setSubject(openid)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 3600 * 1000)) // 1小时有效期
                .signWith(SignatureAlgorithm.HS512, "your_secret_key")
                .compact();
    }
}

